﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using WiTeach.CourseManagement.ViewModel;
using System.Data;
using WiTeach.CourseManagement.Model;
using WiTeach.Tools;
using WiTeach.ClassManagement.Model;
using WiTeach.ClassManagement.DAL;


namespace WiTeach.CourseManagement.DAL
{

    public class CourseDAL
    {

        private static string courseTableName = "ta_course";


        /// <summary>
        /// 根据课程ID 获取对应的课时列表信息
        /// </summary>
        /// <author>Light</author>
        /// <param name="Lesson_ID">课程ID</param>
        /// <returns></returns>
        /// 
        public static List<Class> getClassListByID(int Lesson_ID)
        {

            return ClassDAL.getClassListByID(Lesson_ID);
        }


        #region 数据分页相关

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <author>Light</author>
        /// <param name="pageNow"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<Course> getCurrentPagingList(int pageNow, int pageSize, int pageCount)
        {
            if (pageNow <= 0)
            {
                pageNow = 0;
            }
            string sql = "select * from " + courseTableName + " limit " + (pageNow % pageCount) * pageSize + ", " + pageSize + "";
            DataTable dataTable = SqlHelper.ExecuteDataTable(sql);
            return courseToList(dataTable);
        }

        /// <summary>
        /// 课程模型数据转集合
        /// </summary>
        /// <author>Light</author>
        /// <param name="dataTable"></param>
        /// <returns>模型集合</returns>
        public static List<Course> courseToList(DataTable dataTable)
        {
            List<Course> courseList = new List<Course>();
            if (dataTable == null)
                return null;
            foreach (DataRow dt in dataTable.Rows)
            {
                Course courseInfo = new Course();
                courseInfo.Lesson_ID = (int)dt["id"];
                courseInfo.Lesson_Name = (string)dt["name"];
                //courseInfo.LessonType_ID = (int)dt["LessonType_ID"];
                courseList.Add(courseInfo);
            }
            return courseList;
        }

        /// <summary>
        /// 获取数据表中数据条数
        /// </summary>
        /// <returns></returns>
        public static int getCountByName()
        {
            string strSql = "select count(*) from " + courseTableName;
            return (int)SqlHelper.ExecuteScalar(strSql); ;
        }

        #endregion

        public static string getCourseNameByID(int lessonID)
        {
            string sql = "select name as Lesson_Name from ta_course where id = " + lessonID;
            return (string)SqlHelper.ExecuteScalar(sql);
        }


        /// <summary>
        /// 获取课程列表总条数
        /// </summary>
        /// <returns></returns>
        public static int getCourseCount()
        {
            string strSql = "select count(*) from " + courseTableName;
            object res = SqlHelper.ExecuteScalar(strSql);
            return Convert.ToInt32(res);

            //return BaseDAL.getTotalCount(courseTableName);
        }



    }
}
